package interfacesSIAB;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import administrador.*;

import javax.swing.JCheckBox;
import javax.swing.JToggleButton;

public class BuscarCliente extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JButton buscar1 = null;
	private JLabel jLabel5 = null;
	private JTextField cedulaBuscar = null;
	private JPanel jPanel2 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JTextField nombre = null;
	private JTextField cedula = null;
	private JTextField apellido = null;
	private JTextField telefono = null;
	private JTextField direccion = null;
	private JLabel email = null;
	private JTextField email1 = null;
	private JButton guardar = null;
	private JButton salir = null;
	private JButton editar = null;
	/**
	 * This is the default constructor
	 */
	public BuscarCliente() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(519, 464);
		this.setContentPane(getJPanel());
		this.setResizable(false);
		this.setTitle("Buscar  Cliente");
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJPanel1(), null);
			jPanel.add(getJPanel2(), null);
			jPanel.add(getGuardar(), null);
			jPanel.add(getSalir(), null);
			jPanel.add(getEditar(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(9, 18, 53, 16));
			jLabel5.setText("Cedula");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(17, 20, 331, 59));
			jPanel1.setBackground(new Color(244, 206, 9));
			jPanel1.setBorder(BorderFactory.createLineBorder(Color.orange, 1));
			jPanel1.add(getBuscar1(), null);
			jPanel1.add(jLabel5, null);
			jPanel1.add(getCedulaBuscar(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes buscar1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBuscar1() {
		if (buscar1 == null) {
			buscar1 = new JButton();
			buscar1.setBounds(new Rectangle(230, 15, 82, 24));
			buscar1.setText("Buscar");
			buscar1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					try {
						
									
						if(getCedulaBuscar().getText().trim().length() > 0 )
						{
					
					
					gestion_de_clientes buscar= new gestion_de_clientes();
					
					ArrayList resultado = buscar.buscarCliente(Integer.parseInt(getCedulaBuscar().getText()));
					if(resultado.size()!=0){
						getNombre().setText((String) resultado.get(0));
						getApellido().setText((String) resultado.get(1));
						getCedula().setText( String.valueOf(resultado.get(2)));
						getTelefono().setText(String.valueOf(resultado.get(3)));
						getDireccion().setText((String) resultado.get(4));
						getEmail1().setText((String) resultado.get(5));
						
						 setEstadoEdicion2(false);					
						 getCedula().setRequestFocusEnabled(false); 
						 getNombre().setRequestFocusEnabled(false);
						 getApellido().setRequestFocusEnabled(false);
						 getTelefono().setRequestFocusEnabled(false);
						 getDireccion().setRequestFocusEnabled(false);
						 getEmail1().setRequestFocusEnabled(false);
						
					}
					
		
					
					
						}else{
							
							JOptionPane.showMessageDialog(null,"Por favor Debe de Ingresar la Cedula");
						}
					}
					catch (Exception e1) {
						JOptionPane.showMessageDialog(null,"El siguiente campo no puede contener caracteres"+ e1.getMessage() );
					    e1.printStackTrace();
					    
					 
					}
					
				
				}
			});
		}
		return buscar1;
	}

	/**
	 * This method initializes cedulaBuscar	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCedulaBuscar() {
		if (cedulaBuscar == null) {
			cedulaBuscar = new JTextField();
			cedulaBuscar.setBounds(new Rectangle(102, 17, 99, 20));
		}
		return cedulaBuscar;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			email = new JLabel();
			email.setBounds(new Rectangle(36, 215, 60, 16));
			email.setText("  Email");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(34, 179, 69, 16));
			jLabel4.setText("  Direccion");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(36, 141, 65, 21));
			jLabel3.setText("  Telefono");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(37, 46, 72, 16));
			jLabel2.setText("  Nombre(s)");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(35, 111, 64, 16));
			jLabel1.setText("  Cedula");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(35, 80, 66, 16));
			jLabel.setText("  Apellido(s)");
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.setBounds(new Rectangle(16, 101, 352, 270));
			jPanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.orange), "Cliente", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel2.add(jLabel, null);
			jPanel2.add(jLabel1, null);
			jPanel2.add(jLabel2, null);
			jPanel2.add(jLabel3, null);
			jPanel2.add(jLabel4, null);
			jPanel2.add(getNombre(), null);
			jPanel2.add(getCedula(), null);
			jPanel2.add(getApellido(), null);
			jPanel2.add(getTelefono(), null);
			jPanel2.add(getDireccion(), null);
			jPanel2.add(email, null);
			jPanel2.add(getEmail1(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes nombre	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNombre() {
		if (nombre == null) {
			nombre = new JTextField();
			nombre.setBounds(new Rectangle(157, 42, 158, 20));
			nombre.setEditable(true);
			nombre.setEnabled(false);
		}
		return nombre;
	}

	/**
	 * This method initializes cedula	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCedula() {
		if (cedula == null) {
			cedula = new JTextField();
			cedula.setBounds(new Rectangle(157, 108, 158, 20));
			cedula.setEnabled(false);
		}
		return cedula;
	}

	/**
	 * This method initializes apellido	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getApellido() {
		if (apellido == null) {
			apellido = new JTextField();
			apellido.setBounds(new Rectangle(157, 77, 158, 20));
			apellido.setEnabled(false);
		}
		return apellido;
	}

	/**
	 * This method initializes telefono	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTelefono() {
		if (telefono == null) {
			telefono = new JTextField();
			telefono.setBounds(new Rectangle(157, 138, 158, 20));
			telefono.setEnabled(false);
		}
		return telefono;
	}

	/**
	 * This method initializes direccion	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDireccion() {
		if (direccion == null) {
			direccion = new JTextField();
			direccion.setBounds(new Rectangle(157, 175, 158, 20));
			direccion.setEnabled(false);
		}
		return direccion;
	}

	/**
	 * This method initializes email1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEmail1() {
		if (email1 == null) {
			email1 = new JTextField();
			email1.setBounds(new Rectangle(157, 214, 158, 20));
			email1.setEnabled(false);
		}
		return email1;
	}

	/**
	 * This method initializes guardar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getGuardar() {
		if (guardar == null) {
			guardar = new JButton();
			guardar.setBounds(new Rectangle(385, 151, 83, 25));
			guardar.setEnabled(false);
			guardar.setText("Guardar");
			guardar.addActionListener(new java.awt.event.ActionListener() {   
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					
					try {
						
						 
						
						
						if(getNombre().getText().trim().length() > 0 )
						{
							if(getApellido().getText().trim().length() > 0)
							{
								
								if(getCedula().getText().trim().length() > 0)
								
								{
																	
									gestion_de_clientes actualizar= new gestion_de_clientes();
									actualizar.actualizarInfoCliente(getNombre().getText(),getApellido().getText(),Integer.parseInt(getCedula().getText()),Integer.parseInt(getTelefono().getText()),getDireccion().getText(),getEmail1().getText());
								
									
									setEstadoEdicion2(false);					
									getEditar().setRequestFocusEnabled(true);
									getCedula().setRequestFocusEnabled(false); 
									 getNombre().setRequestFocusEnabled(false);
									 getApellido().setRequestFocusEnabled(false);
									 getTelefono().setRequestFocusEnabled(false);
									 getDireccion().setRequestFocusEnabled(false);
									 getEmail1().setRequestFocusEnabled(false);
							         
									 
									
								}
								else{
									JOptionPane.showMessageDialog(null,"Por favor Debe de Ingresar la Cedula");
								}
								
								
							}
							else{
								JOptionPane.showMessageDialog(null,"Por favor Debe de Ingresar el Apellido");
							}
							
							
							
							
							
						}
						else{
							
							JOptionPane.showMessageDialog(null,"Por favor Debe de Ingresar el Nombre");
						}
						
						
						}
						catch (Exception e1) {
							JOptionPane.showMessageDialog(null,"El siguiente campo no puede contener caracteres"+ e1.getMessage() );
						    e1.printStackTrace();
						}
				
				
				
				
				
				
				
				
				}
			
			});
		}
		return guardar;
	}

	/**
	 * This method initializes salir	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getSalir() {
		if (salir == null) {
			salir = new JButton();
			salir.setBounds(new Rectangle(385, 194, 83, 28));
			salir.setText("Salir");
			
salir.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					
					dispose();
				}
			});
			
		
			
			
			
			
			
			
		}
		return salir;
	}

	/**
	 * This method initializes editar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getEditar() {
		if (editar == null) {
			editar = new JButton();
			editar.setBounds(new Rectangle(385, 114, 83, 25));
			editar.setEnabled(false);
			editar.setText("Editar");
			editar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					
					setEstadoEdicion(true);
					
					getCedulaBuscar().setRequestFocusEnabled(true);
				
					getNombre().setRequestFocusEnabled(true);
					getApellido().setRequestFocusEnabled(true);
				    getCedula().setRequestFocusEnabled(false);
				    getTelefono().setRequestFocusEnabled(true);
				    getDireccion().setRequestFocusEnabled(true);
				    getEmail1().setRequestFocusEnabled(true);
				    
				    
				    getGuardar().setRequestFocusEnabled(false);
				   
				    
				    
				    
					
					
				}
			});
		}
		return editar;
	}

	
	
public void  setEstadoEdicion(boolean isEdicion) {
		
		getCedulaBuscar().setEnabled( ! isEdicion);
	    getNombre().setEnabled( isEdicion);
		getApellido().setEnabled(isEdicion);
		getCedula().setEnabled( ! isEdicion);
		getTelefono().setEnabled( isEdicion);
		getDireccion().setEnabled( isEdicion);
		getEmail1().setEnabled( isEdicion);
		
	
		
		
		getGuardar().setEnabled( isEdicion);
	    getEditar().setEnabled( ! isEdicion);
	  
		
		
		
		
		
	}




public void  setEstadoEdicion2(boolean isEdicion) {
	
	getCedulaBuscar().setEnabled( ! isEdicion);
	getCedulaBuscar().selectAll();
    getNombre().setEnabled( !isEdicion);
	getApellido().setEnabled(!isEdicion);
	getCedula().setEnabled( ! isEdicion);
	getTelefono().setEnabled( !isEdicion);
	getDireccion().setEnabled( !isEdicion);
	getEmail1().setEnabled( !isEdicion);
	
   
	
	
	getGuardar().setEnabled( isEdicion);
    getEditar().setEnabled( ! isEdicion);
  
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	
}  //  @jve:decl-index=0:visual-constraint="36,7"
